import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsNumber,
  IsEnum,
  IsArray,
  MaxLength,
  Min,
  Max,
  Matches
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { Type } from 'class-transformer';

// 创建标签 DTO
export class CreateTagDto {
  @ApiProperty({
    description: '标签名称',
    example: 'NestJS',
    maxLength: 50
  })
  @IsString()
  @IsNotEmpty()
  @MaxLength(50)
  name: string;

  @ApiPropertyOptional({
    description: '标签颜色',
    example: '#1890ff',
    default: '#1890ff'
  })
  @IsOptional()
  @IsString()
  @Matches(/^#[0-9A-Fa-f]{6}$/, {
    message: '颜色格式应为 #RRGGBB'
  })
  color?: string;

  @ApiPropertyOptional({
    description: '标签描述',
    example: '一个用于构建高效、可扩展的 Node.js 应用程序的框架',
    maxLength: 255
  })
  @IsOptional()
  @IsString()
  @MaxLength(255)
  description?: string;

  @ApiPropertyOptional({
    description: '使用计数',
    example: 10,
    default: 0
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  count?: number;

  @ApiPropertyOptional({
    description: '状态',
    example: 1,
    enum: [0, 1],
    default: 1
  })
  @IsOptional()
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '排序',
    example: 0,
    default: 0
  })
  @IsOptional()
  @IsNumber()
  sort?: number;

  @ApiPropertyOptional({
    description: 'URL 别名',
    example: 'nestjs',
    maxLength: 100
  })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  slug?: string;

  @ApiPropertyOptional({
    description: 'SEO 标题',
    maxLength: 255
  })
  @IsOptional()
  @IsString()
  @MaxLength(255)
  seoTitle?: string;

  @ApiPropertyOptional({
    description: 'SEO 描述'
  })
  @IsOptional()
  @IsString()
  seoDescription?: string;

  @ApiPropertyOptional({
    description: 'SEO 关键词',
    maxLength: 500
  })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  seoKeywords?: string;
}

// 更新标签 DTO
export class UpdateTagDto extends PartialType(CreateTagDto) {}

// 查询标签 DTO
export class QueryTagsDto {
  @ApiPropertyOptional({
    description: '页码',
    example: 1,
    minimum: 1,
    default: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @ApiPropertyOptional({
    description: '每页数量',
    example: 20,
    minimum: 1,
    maximum: 100,
    default: 20
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  @Max(100)
  limit?: number = 20;

  @ApiPropertyOptional({
    description: '搜索关键词（标签名、描述）',
    example: 'nest'
  })
  @IsOptional()
  @IsString()
  search?: string;

  @ApiPropertyOptional({
    description: '状态',
    enum: [0, 1]
  })
  @IsOptional()
  @Type(() => Number)
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '最小使用次数',
    example: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(0)
  minCount?: number;

  @ApiPropertyOptional({
    description: '最大使用次数',
    example: 100
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(0)
  maxCount?: number;

  @ApiPropertyOptional({
    description: '排序字段',
    example: 'count',
    enum: ['createdAt', 'updatedAt', 'name', 'count', 'sort']
  })
  @IsOptional()
  @IsString()
  sortBy?: string = 'count';

  @ApiPropertyOptional({
    description: '排序方向',
    example: 'DESC',
    enum: ['ASC', 'DESC'],
    default: 'DESC'
  })
  @IsOptional()
  @IsString()
  sortOrder?: 'ASC' | 'DESC' = 'DESC';
}

// 批量操作标签 DTO
export class BatchTagActionDto {
  @ApiProperty({
    description: '标签ID数组',
    type: [Number],
    example: [1, 2, 3]
  })
  @IsArray()
  @IsNumber({}, { each: true })
  tagIds: number[];

  @ApiProperty({
    description: '操作类型',
    enum: ['activate', 'deactivate', 'delete', 'merge']
  })
  @IsString()
  @IsEnum(['activate', 'deactivate', 'delete', 'merge'])
  action: 'activate' | 'deactivate' | 'delete' | 'merge';

  @ApiPropertyOptional({
    description: '目标标签ID（用于合并操作）',
    example: 1
  })
  @IsOptional()
  @IsNumber()
  targetTagId?: number;
}

// 合并标签 DTO
export class MergeTagsDto {
  @ApiProperty({
    description: '源标签ID数组',
    type: [Number],
    example: [2, 3, 4]
  })
  @IsArray()
  @IsNumber({}, { each: true })
  sourceTagIds: number[];

  @ApiProperty({
    description: '目标标签ID',
    example: 1
  })
  @IsNumber()
  targetTagId: number;
}

// 标签统计 DTO
export class TagStatsDto {
  @ApiPropertyOptional({
    description: '统计类型',
    example: 'usage',
    enum: ['usage', 'popular', 'recent'],
    default: 'usage'
  })
  @IsOptional()
  @IsEnum(['usage', 'popular', 'recent'])
  type?: 'usage' | 'popular' | 'recent';

  @ApiPropertyOptional({
    description: '数量限制',
    example: 10,
    minimum: 1,
    maximum: 100,
    default: 10
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  @Max(100)
  limit?: number = 10;
}
